package com.hoyin.lightctrl_esp8266;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.FileNotFoundException;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {
	private SocketConnectThread mSocketConnectThread;
	private SocketSendThread mSocketSendThread;
	private ConstraintLayout mainPage;
	private LocalConfigIO mLocalConfigIO;
	private JsonConfig mJsonConfig = new JsonConfig();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		JsonConfig configContent;
		setContentView(R.layout.activity_main);
		mainPage = findViewById(R.id.MainPage);
		mLocalConfigIO = new LocalConfigIO(this.getApplicationContext());
		mLocalConfigIO.AttachFile("connection_config.json");
		if (!mLocalConfigIO.IsConfigExist()) mLocalConfigIO.CreateConfigFile();

		Log.i("Config name", mLocalConfigIO.GetName());
		Log.i("Config path", mLocalConfigIO.GetPath());
		configContent = mLocalConfigIO.ReadJsonConfig();

		Log.i("Config content", configContent.ipAddress);
		Log.i("Config content", configContent.port);

		this.runOnUiThread(new Runnable() {
			EditText v_light_ip = findViewById(R.id.editTxt_ip);
			EditText v_light_port = findViewById(R.id.editTxt_port);
			public void run() {
				v_light_ip.setText(configContent.ipAddress);
				v_light_port.setText(configContent.port);
			}
		});
	}

	public void btn_connect__onclick(View view) throws UnknownHostException {
		EditText v_light_ip = findViewById(R.id.editTxt_ip);
		EditText v_light_port = findViewById(R.id.editTxt_port);
		String light_ip = String.valueOf(v_light_ip.getText());
		String light_port = String.valueOf(v_light_port.getText());
		Log.i("HHH", light_ip);
		mJsonConfig.ipAddress = light_ip;
		mJsonConfig.port = light_port;
		if (mLocalConfigIO.IsConfigExist()) {
			mLocalConfigIO.WriteJsonConfig(mJsonConfig);
		}
		mSocketConnectThread = new SocketConnectThread(this, light_ip, light_port);
		mSocketConnectThread.start();
	}

	public void btn_lightOff__onclick(View view) {
		mSocketSendThread = new SocketSendThread(mSocketConnectThread.getOutStream());
		mSocketSendThread.storeMsg("control_command:0x02");
		mSocketSendThread.start();
		mainPage.setBackgroundResource(R.drawable.light_off);
	}

	public void btn_lightOn__onclick(View view) {
		mSocketSendThread = new SocketSendThread(mSocketConnectThread.getOutStream());
		mSocketSendThread.storeMsg("control_command:0x01");
		mSocketSendThread.start();
		mainPage.setBackgroundResource(R.drawable.light_on);
	}

	public void btn_ledOn__onclick(View view) {
		mSocketSendThread = new SocketSendThread(mSocketConnectThread.getOutStream());
		mSocketSendThread.storeMsg("control_command:0x03");
		mSocketSendThread.start();
	}

	public void btn_ledOff__onclick(View view) {
		mSocketSendThread = new SocketSendThread(mSocketConnectThread.getOutStream());
		mSocketSendThread.storeMsg("control_command:0x04");
		mSocketSendThread.start();
	}
}